package com.example.demo.utils;

import com.example.demo.dto.UserDTO;
import com.example.demo.entity.User;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class EntityToDTO {
    public static UserDTO EntirytoDTO(User user){
        String sex = "";
        String role = "";
        if (user == null){
            return null;
        }
        if ("1".equals(user.getUsex())){
            sex = "男";
        } else if ("0".equals(user.getUsex())){
            sex = "女";
        }
        if ("1".equals(user.getUrole())){
            role = "超级管理员";
        } else if ("0".equals(user.getUrole())){
            role = "管理员";
        }
        return new UserDTO(user.getUid(),user.getUsername(),user.getUpassword(),user.getImg(),sex,user.getUage(),role);
    }
    public static List<UserDTO> EntiryListtoDTO(List<User> lists){
        List<UserDTO> userDTOList = new ArrayList<UserDTO>();
        if (lists.isEmpty() || lists.size() <= 0 || lists == null){
            return Collections.emptyList();
        }
        for (User user : lists){
            String sex = "";
            String role = "";
            if (user == null){
                return null;
            }
            if ("1".equals(user.getUsex())){
                sex = "男";
            } else if ("0".equals(user.getUsex())){
                sex = "女";
            }
            if (user.getUrole() == 1){
                role = "超级管理员";
            } else if (user.getUrole() == 0){
                role = "管理员";
            }

            userDTOList.add(new UserDTO(user.getUid(),user.getUsername(),user.getUpassword(),user.getImg(),sex,user.getUage(),role));
        }
        return userDTOList;
    }
}
